package org.zuma.gradle.springboot;

import io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.tasks.testing.Test;

/**
 * @author Lee
 * @date 2024/10/10 09:56
 */
public class SpringBootBomAction {

    public static void config(Project project) {
        var pluginContainer = project.getPlugins();
        pluginContainer.apply("org.springframework.boot");
        pluginContainer.apply("io.spring.dependency-management");
    }

    public static void enableSpringJunitTest(Project project) {
        var dependencyHandler = project.getDependencies();
        dependencyHandler.add("testImplementation", "org.springframework.boot:spring-boot-starter-test");
        dependencyHandler.add("testRuntimeOnly", "org.junit.platform:junit-platform-launcher");
        project.getTasks().named("test", Test.class).configure(Test::useJUnitPlatform);
    }

    public static void enableLombok(Project project) {
        var dependencyHandler = project.getDependencies();
        dependencyHandler.add("compileOnly", "org.projectlombok:lombok");
        dependencyHandler.add("annotationProcessor", "org.projectlombok:lombok");
        dependencyHandler.add("testCompileOnly", "org.projectlombok:lombok");
        dependencyHandler.add("testAnnotationProcessor", "org.projectlombok:lombok");
    }

    public static void enableConfigProcessor(Project project) {
        var dependencyHandler = project.getDependencies();
        dependencyHandler.add("annotationProcessor", "org.springframework.boot:spring-boot-configuration-processor");
        project.getConfigurations().getByName("compileOnly").extendsFrom(project.getConfigurations().getByName("annotationProcessor"));
    }

    public static void enableDevTools(Project project) {
        var dependencyHandler = project.getDependencies();
        dependencyHandler.add("developmentOnly", "org.springframework.boot:spring-boot-devtools");
    }

    public static void enableSpringCloud(Project project, String cloudVersion) {
        project.getExtensions().configure("dependencyManagement", (Action<DependencyManagementExtension>) o -> o.imports(importsHandler -> {
            importsHandler.mavenBom("org.springframework.cloud:spring-cloud-dependencies:" + cloudVersion);
        }));
    }
}
